package com.ys.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.extend.common.model.AjaxJson;
import com.ys.web.entity.BestKeyword;
import com.ys.web.pojo.BestKeywordInfo;
import com.ys.web.service.BestKeywordService;

/**
 * 行业最优关键词方案
 * @author wdj
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/bestKeywordController")
public class BestKeywordController {
	
	@Autowired
	private BestKeywordService bestKeywordService;
	
	
	@RequestMapping(params="showBestKeyword")
	public ModelAndView showBestKeyword(HttpServletRequest request){	
		return new ModelAndView("bestKeyword/bestKeywordList");
	}
	
	/**
	 * 根据行业查询行业最优关键词方案
	 * @param page 当前页码 
	 * @param industryId 行业id
	 * @return 行业最优关键词方案列表
	 */
	@RequestMapping(params="getBestKeywordByIndustry")
	@ResponseBody
	public List<BestKeywordInfo> getBestKeywordByIndustry(Integer page, String industryId) {
		//暂时页面没传分页码
		Integer page_par = 1;
		if(page!=null){
			page_par = page;
		}
		Integer industryId_par = null;
		if(industryId!=null && industryId.length()>0) {
			industryId_par = Integer.valueOf(industryId);
		}
		
		return bestKeywordService.getBestKeywordByIndustry(industryId_par, page_par);
	}
	
	/**
	 * 批量保存新增的最优行业关键词，不存在，才保存新增
	 * @param bestKeywordList 最优行业关键词列表
	 * @return
	 */
	@RequestMapping(params="batchAdd")
	@ResponseBody
	public AjaxJson batchAdd(@RequestBody BestKeyword[] bestKeywordList) {
		AjaxJson j = new AjaxJson();
		j.setMsg("保存成功");
		
		if(bestKeywordList!=null && bestKeywordList.length>0) {							
			try {
				//批量保存
				Boolean ifSave = bestKeywordService.batchAdd(bestKeywordList);				
				if(!ifSave) {
					j.setSuccess(false);				
					j.setMsg("保存失败，已存在相同关键词方案");						
				}
			} catch (Exception e) {
				j.setSuccess(false);				
				j.setMsg("保存失败");			
				e.printStackTrace();
			}
		}
		else {
			j.setSuccess(false);
			j.setMsg("传递的参数为空");			
		}
		
		return j;		
	}
	
	/**
	 * 除删除同一行业下同一标题的“最优行业关键词”
	 * @param id
	 * @return
	 */
	@RequestMapping(params="batchDelete")
	@ResponseBody
	public AjaxJson batchDelete(Integer id) {
		AjaxJson j = new AjaxJson();
		j.setMsg("删除成功");
		
		try {
			bestKeywordService.deleteBestKeywordByTitle(id);
			
		} catch (Exception e) {
			j.setSuccess(false);				
			j.setMsg("删除失败，异常");
			e.printStackTrace();
		}
		
		return j;		
	}
	
	
	
	
}
